package com.zw.yzk.kabu.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseApplication;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.internal.component.DaggerWXPayComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.QueryOrderStateModule;
import com.zw.yzk.kabu.management.ActivityManager;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.presenter.WXPayPresenter;
import com.zw.yzk.kabu.utils.WechatPayInfo;
import com.zw.yzk.kabu.view.activity.GoodsOrderDetailsActivity;
import com.zw.yzk.kabu.view.activity.MyGoodsOrderActivity;
import com.zw.yzk.kabu.view.activity.TicketOrderDetailsActivity;
import com.zw.yzk.kabu.view.view.WXPayView;

import javax.inject.Inject;

/**
 * Created by zhanwei on 2017/2/28.
 */

public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler, WXPayView {

    private IWXAPI api;

    @Inject
    WXPayPresenter presenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        api = WXAPIFactory.createWXAPI(this, WechatManager.WECHAT_APP_ID);
        api.handleIntent(getIntent(), this);

        initPresenter();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.destroy();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);

        if (presenter == null) {
            initPresenter();
        }
    }


    @Override
    public void onReq(BaseReq baseReq) {
        //sendReq是第三方app主动发送消息给微信，发送完成之后会切回到第三方app界面。
        finish();
    }

    @Override
    public void onResp(BaseResp baseResp) {
        //sendResp是微信向第三方app请求数据，第三方app回应数据之后会切回到微信界面。
//        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//            if (baseResp.errCode == 0) {
//                //支付成功
//                LoadingDialogManager.getInstance().showLoadingDialog(this);
//                if (presenter == null) {
//                    initPresenter();
//                }
//                presenter.checkOrderState(WechatPayInfo.getInstance().getTradeId());
//            } else if (baseResp.errCode == -1) {
//                //支付失败
//                ToastManager.getInstance().showToast(this, getResources().getString(R.string.pay_failed));
//                finish();
//            } else {
//                finish();
//            }
//        } else {
//            finish();
//        }
        finish();
    }

    @Override
    public void queryOrderStateSuccess() {
        if (WechatPayInfo.getInstance().getPaymentType() == WechatPayInfo.PAYMENT_TYPE_GOODS) {
            startActivity(new Intent(this, MyGoodsOrderActivity.class));
        } else if (WechatPayInfo.getInstance().getPaymentType() == WechatPayInfo.PAYMENT_TYPE_TICKET) {
            if (!WechatPayInfo.getInstance().getOrderIdList().isEmpty()) {
                Intent intent = new Intent(this, TicketOrderDetailsActivity.class);
                intent.putExtra(ActivityConstant.EXHIBITION_TICKET_ORDER_ID, WechatPayInfo.getInstance().getOrderIdList().get(0));
                startActivity(intent);
            }
        }
        finish();
    }

    @Override
    public void queryOrderStateFailed() {
        ToastManager.getInstance().showToast(this, getResources().getString(R.string.check_order_state_by_yourself));
        finish();
    }

    private void initPresenter() {
        DaggerWXPayComponent.builder()
                .applicationComponent(BaseApplication.getInstance().getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .queryOrderStateModule(new QueryOrderStateModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }
}
